package com.android.gtdroid;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBTaskAdapter {
	private final String TABLE_NAME = "task"; // task table

	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "task_name";
	public static final String KEY_NOTE = "task_note";
	public static final String KEY_LIST = "task_list";
	public static final String KEY_CONTEXT = "task_context";
	public static final String KEY_PROJECT = "task_project";
	public static final String KEY_POS = "task_pos";
	public static final String KEY_STATUS = "task_status";
	public static final String KEY_ST = "task_st";
	public static final String KEY_ET = "task_et";
	public static final String KEY_REPEAT = "task_repeat";
	public static final String KEY_REMIND = "task_remind";
	public static final String KEY_PRIOR = "task_prior";
	public static final String KEY_TAG = "task_tag";
	public static final String KEY_COMPLETED = "task_completed";
	public static final String KEY_CREATED = "task_created";
	
	public static final int COL_ID = 0;
	public static final int COL_TASK_NAME = 1;
	public static final int COL_TASK_NOTE = 2;
	public static final int COL_TASK_LIST = 3;
	public static final int COL_TASK_CONTEXT = 4;
	public static final int COL_TASK_PROJECT = 5;
	public static final int COL_TASK_POS = 6;
	public static final int COL_TASK_STATUS = 7;
	public static final int COL_TASK_ST = 8;
	public static final int COL_TASK_ET = 9;
	public static final int COL_TASK_REPEAT = 10;
	public static final int COL_TASK_REMIND = 11;
	public static final int COL_TASK_PRIOR = 12;
	public static final int COL_TASK_TAG = 13;
	public static final int COL_TASK_COMPLETED = 14;
	public static final int COL_TASK_CREATED = 15;
	
	private SQLiteDatabase db;

	
	
	public String getTableName() {
		return TABLE_NAME;
	}
	
	public DBTaskAdapter(SQLiteDatabase db) {
		this.db = db;
	}

	/*
	 * Insert a new item
	 */
	public long insertTask(TaskItem _task) {
		// Create a new row of values to insert.
		ContentValues newTaskVal = new ContentValues();
		// Assign values for each row.
		newTaskVal.put(KEY_NAME, _task.getTaskName());
		newTaskVal.put(KEY_NOTE, _task.getTaskNote());
		newTaskVal.put(KEY_LIST, _task.getListName());
		newTaskVal.put(KEY_CONTEXT, _task.getContext());
		newTaskVal.put(KEY_PROJECT, _task.getProject());
		newTaskVal.put(KEY_POS, _task.getPosition());
		if (_task.isAllDay())
			newTaskVal.put(KEY_STATUS, 1);
		else
			newTaskVal.put(KEY_STATUS, 0);
		newTaskVal.put(KEY_ST, _task.getStartTime());
		newTaskVal.put(KEY_ET, _task.getEndTime());
		newTaskVal.put(KEY_REPEAT, _task.getRepeat());
		newTaskVal.put(KEY_REMIND, _task.getRemind());
		newTaskVal.put(KEY_PRIOR, _task.getPrior());
		newTaskVal.put(KEY_TAG, _task.getTags());
		newTaskVal.put(KEY_COMPLETED, _task.getCompletedStatus());
		newTaskVal.put(KEY_CREATED, _task.getCreated());

		return db.insert(TABLE_NAME, null, newTaskVal);
	}

	/*
	 * Remove a list based on its index
	 */
	public boolean removeTask(long _rowIndex) {
		return db.delete(TABLE_NAME, KEY_ID + "=" + _rowIndex, null) > 0;
	}
	
	/*
	 * Remove all lists
	 */
	public boolean removeAllTask() {
		return db.delete(TABLE_NAME, null, null) > 0;
	}

	/*
	 * Update a list
	 */
	public boolean updateTask(long _rowIndex, TaskItem _task) {
		ContentValues newValue = new ContentValues();
		newValue.put(KEY_NAME, _task.getTaskName());
		newValue.put(KEY_NOTE, _task.getTaskNote());
		newValue.put(KEY_LIST, _task.getListName());
		newValue.put(KEY_CONTEXT, _task.getContext());
		newValue.put(KEY_PROJECT, _task.getProject());
		newValue.put(KEY_POS, _task.getPosition());
		if (_task.isAllDay())
			newValue.put(KEY_STATUS, 1);
		else
			newValue.put(KEY_STATUS, 0);
		newValue.put(KEY_ST, _task.getStartTime());
		newValue.put(KEY_ET, _task.getEndTime());
		newValue.put(KEY_REPEAT, _task.getRepeat());
		newValue.put(KEY_REMIND, _task.getRemind());
		newValue.put(KEY_PRIOR, _task.getPrior());
		newValue.put(KEY_TAG, _task.getTags());
		newValue.put(KEY_COMPLETED, _task.getCompletedStatus());
		newValue.put(KEY_CREATED, _task.getCreated());
		
		return db.update(TABLE_NAME, newValue, KEY_ID + "=" + _rowIndex, null) > 0;
	}
	

	/*
	 * Cursor operations
	 */
	public Cursor getAllTaskItems() {
		return db.query(TABLE_NAME, null, null, null, null, null, null);
	}
	
	public Cursor fetchTasks(String where) throws SQLException {
		Cursor csr = db.query(TABLE_NAME, null, where, null, null, null, null, null);
		if (csr != null)
			csr.moveToFirst();
		
		return csr; 
	}
	
	public Cursor excuteSQL(String sql) throws SQLException {
		return db.rawQuery(sql, null);
	}

	public Cursor setCursorToTaskItem(long _rowIndex) throws SQLException {
		Cursor result = db.query(true,
				TABLE_NAME,
				null,
				KEY_ID + "=" + _rowIndex,
				null, null, null, null, null);
		if ((result.getCount() == 0) || !result.moveToFirst()) {
			throw new SQLException("No task found for row: " + _rowIndex);
		}
		return result;
	}
	
	public TaskItem getTaskItem(long _rowIndex) throws SQLException {
		Cursor cursor = db.query(true, TABLE_NAME,
				null, KEY_ID + "=" + _rowIndex,
				null, null, null, null, null);
		if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
			throw new SQLException("No to do item found for row: " + _rowIndex);
		}
		String task = cursor.getString(COL_TASK_NAME);
		String note = cursor.getString(COL_TASK_NOTE);
		String list = cursor.getString(COL_TASK_LIST);
		String ctt = cursor.getString(COL_TASK_CONTEXT);
		String prj = cursor.getString(COL_TASK_PROJECT);
		String pos = cursor.getString(COL_TASK_POS);
		int allDay = cursor.getInt(COL_TASK_STATUS);
		String st = cursor.getString(COL_TASK_ST);
		String et = cursor.getString(COL_TASK_ET);
		String repeat = cursor.getString(COL_TASK_REPEAT);
		String remind = cursor.getString(COL_TASK_REMIND);
		String prior = cursor.getString(COL_TASK_PRIOR);
		String tag = cursor.getString(COL_TASK_TAG);
		int completed = cursor.getInt(COL_TASK_COMPLETED);
		String created = cursor.getString(COL_TASK_CREATED);
		
		TaskItem result = new TaskItem(task, note, list);
		result.setContext(ctt);
		result.setProject(prj);
		result.setPosition(pos);
		if (allDay == 1)
			result.setAllDay(true);
		else
			result.setAllDay(false);
		result.setStartTime(st);
		result.setEndTime(et);
		result.setRepeat(repeat);
		result.setRemind(remind);
		result.setPrior(prior);
		result.setTags(tag);
		result.setCompletedStatus(completed);
		/*if (completed == 1)
			result.setCompleted(true);
		else
			result.setCompleted(false);*/
		result.setCreated(created);
		
		return result;
	}
}